<?php
declare (strict_types=1);

namespace app\user\service;


use app\model\Chat;
use app\model\ChatLog;
use app\model\user\User;

class UserService
{
    /**
     * 用户登录
     * @param $data
     * @return mixed|string
     * @author dgc<453342213@qq.com>
     * createTime:2020/9/2 14:17
     */
    public function Login($data)
    {
        $where[] = ['username|phone', '=', $data['username']];
        $info = $this->getInfo($where, 'id,token,phone,head,password,status,salt');
        //删除之前的缓存
        if (!$info) {
            throw  new \Exception('用户不存在或密码错误', -1);
        }
        if ($info->status != 1) {
            throw  new \Exception('账号已被禁用，如有需要请联系管理员！', -1);
        }
        if ($info->password != setPassword($data['password'], $info->salt)) {
            throw  new \Exception('用户不存在或密码错误', -1);
        }
        $toInfo = $info->toArray();
        unset($toInfo['password']);
        unset($toInfo['token']);
        unset($toInfo['salt']);
        //删除之前的缓存
        redisHdel('login_info', "user_" . $info->token);
        $info->token = md5(sha1($info->id . time() . rand(1, 9999)));
        $info->ip = getClientIp();
        $info->save();
        redisHset('login_info', "user_" . $info->token, $toInfo);
        return $info->token;
    }

    /**
     * 获取用户
     * @param $where
     * @param string $field
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author dgc<453342213@qq.com>
     * createTime:2020/9/2 14:11
     */
    public function getInfo($where, $field = "*")
    {
        return (new User())->where($where)
            ->field($field)
            ->find();
    }

    /**
     * 获取聊天记录
     * @param $chatId 聊天id
     * @return array
     * @author dgc<453342213@qq.com>
     * createTime:2020/8/24 14:24
     */
    public function chatLogList($user, $data)
    {
        //查询是否存在
        $info = (new Chat())->where(['id' => $data['chat_id']])
            ->where('user_id', $user['id'])
            ->find();
        if (is_null($info)) {
            return [];
        }
        $page = getPage();
        //查询id
        $model = new ChatLog();
        $list = $model->where('chat_id',  $data['chat_id'])
            ->page($page['page'], $page['list_rows'])
            ->order('id desc')
            ->select()
            ->toArray();
        sort($list);
        return $list;
    }

    /**
     * 更新用户聊天队列
     * @param $data
     * @return bool
     */
    public static function saveChat($where, $data)
    {
        $model = new Chat();
        $info = $model->where($where)->find();
        if (is_null($info)) {
            return $model->save($data);
        }
        $info->save($data);
        return $info->toArray();
    }

    /**
     * 跟新用户信息
     * @param $data
     * @return bool
     */
    public static function updateUser($data)
    {
        return User::where('id', $data['id'])->save($data);
    }
}
